﻿window.onload = initAll;

function initAll() {
  // Hide the Tips div
  hideTips();
  // Load Amap API
  loadScript();
}

function loadScript(scriptURL) {
  var dtd = Yuan.Deferred();
  var url = scriptURL || "http://webapi.amap.com/maps?v=1.2&key=97b27bfcd3d40ec66b150c4acb673034&callback=initMap";
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.src = url;
  document.body.appendChild(script);
  dtd.resolve();
  return dtd.promise();
}

function showTips(message) {
  var div = document.getElementById("tipsDiv");
  if (message) {
    div.innerHTML = message;
  }
  div.style.display = "block";
}

function hideTips() {
  var div = document.getElementById("tipsDiv");
  div.style.display = "none";
}

function initMap() {
  // try to get a user's location
  if (navigator.geolocation) {
    var positionOption = {
      enableHighAccuracy: true,
      timeout: Infinity,
      maximumAge: 0
    };
    navigator.geolocation.getCurrentPosition(getCurrentPositionSuccess, getCurrentPositionError, positionOption);
  } else {
    showMap();
  }
}

function getCurrentPositionSuccess(position) {
  var mapOptions = {
    level: 15,
    center: new AMap.LngLat(position.coords.longitude, position.coords.latitude)
  };
  showMap(mapOptions).then(function () {
    alert('show ok1');
    // add center marker
    addCenterMarker();
    loadPlugins();
  }, function () {
    alert('show fail1');
  });
}

function loadPlugins() {
  var mapObj = YuanMap.map;
  
  //loadScript('findLocation.js').then(function () {
  //  alert("find location load ok");
  //  var homeControl = new YuanMap.findLocation();
  //  mapObj.addControl(homeControl);                 

  //}, function () {
  //  alert("find location load failed");
  //});
  
  $.getScript('findLocation.js').then(function () {
    var homeControl = new AMap.findLocation();
    mapObj.addControl(homeControl);
  }, function () {
    debugger;
  });


}

function addCenterMarker() {
  alert('addCenterMarker');
  
  var marker = new AMap.Marker({
    map: YuanMap.map,
    position: YuanMap.map.getCenter(),
    offset: new AMap.Pixel(-10, -34),
    icon: new AMap.Icon({
      image: "images/6_circle.png",
      size: new AMap.Size(10,10),
      imageOffset: new AMap.Pixel(0, 0)
    })
  });
  YuanMap.centerMarker = marker;
  YuanMap.markers.push(marker);
}

function getCurrentPositionError(error) {
  switch (error.code) {
    case error.PERMISSION_DENIED:
      alert("User denied the request for Geolocation.");
      break;
    case error.POSITION_UNAVAILABLE:
      alert("Location information is unavailable.");
      break;
    case error.TIMEOUT:
      alert("The request to get user location timed out.");
      break;
    case error.UNKNOWN_ERROR:
      alert("An unknown error occurred.");
      break;
  }
  showMap().then(function () {
    alert('show ok');
    loadPlugins();
  }, function () {
    alert('show fail');
  });
}

function showMap(mapOptions) {
  var dtd = Yuan.Deferred();
  var mapOptions = mapOptions || {
    level: 12,
    center: new AMap.LngLat(116.404,39.915)
  };
  var map = new AMap.Map('container', mapOptions);
  YuanMap.map = map;
  dtd.resolve();
  return dtd.promise();
}